<template>
  <div>
    <template>
      <el-table
        :data="userList.filter(data => !search || data.name.toLowerCase().includes(search.toLowerCase()))"
        style="width: 100%">
        <el-table-column
          label="ID"
          prop="id"
          width="100">
        </el-table-column>

        <el-table-column
          label="名字"
          prop="name"
          width="100">
        </el-table-column>

        <el-table-column
          label="性别"
          prop="sex"
          width="100">
        </el-table-column>

        <el-table-column
          prop="birthday"
          label="出生日期">
        </el-table-column>

        <el-table-column
          prop="registTime"
          label="注册日期">
        </el-table-column>


        <el-table-column
          align="right">
          <template slot="header" slot-scope="scope">
            <el-input
              v-model="search"
              size="mini"
              placeholder="输入名字搜索"/>

            <el-button
              size="mini"
              type="primary"
              @click="handleInsert()">新增用户
            </el-button>

          </template>
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleLook(scope.row)">查看
            </el-button>
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>

    <el-dialog
      title="员工信息"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose">
      <p>ID:{{ userLook.id }}</p>
      <p>姓名:{{ userLook.name }}</p>
      <p>性别:{{ userLook.sex }}</p>
      <p>出生日期:{{ userLook.birthday }}</p>
      <p>注册日期:{{ userLook.registTime }}</p>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Home",
  data() {
    return {
      user: this.$store.getters.getUser,
      userList: [],
      search: '',
      dialogVisible: false,
      userLook: {
        id: 0,
        name: '',
        sex: '',
        birthday: '',
        registTime: '',
      }
    }
  },
  methods: {
    //点击
    handleLook(row) {
      //显示弹框
      this.userLook = row;
      this.dialogVisible = true;
    },

    handleInsert() {
      this.$router.push('/userInsert');
    },
    handleEdit(row) {
      this.$router.push(`/userEdit/${row.id}`);
    },
    handleDelete(row) {
      let vm = this;
      this.axios({
        url: `http://localhost:9090/springmvc02_war_exploded/vue/users/${row.id}`,
        method: 'GET',
      }).then(function (res) {
        //判断是否删除成功
        if (res.data.code == 0) {
          vm.$message.success('删除成功');
          //删除成功后，重新查询用户
          vm.getUserList();
        }
      }).catch(function (err) {
        vm.$message.error('删除失败');

      });
    },
    getUserList() {
      let vm = this;
      this.axios({
        url: 'http://localhost:9090/springmvc02_war_exploded/vue/users',
        method: 'GET',
      }).then(function (res) {
        console.log(res);
        vm.userList = res.data.data.list;
      });
    }
  },
  mounted() {
    //页面挂载完成后，查询用户
    this.getUserList();
  }
}
</script>

<style scoped>

</style>
